package com.haoshuang.zhouzhoubang.ui.page.welfare.push

import android.content.Context
import android.os.Bundle
import com.haoshuang.zhouzhoubang.databinding.ActivityPushOrderWelfareBinding
import com.haoshuang.zhouzhoubang.model.bean.FreeOrderWelfareBean
import com.haoshuang.zhouzhoubang.model.bean.PushOrderWelfareBean
import com.haoshuang.zhouzhoubang.model.bean.ShopWelfareRecordBean
import com.haoshuang.zhouzhoubang.ui.page.welfare.free.FreeOrderWelfareAdapter
import com.haoshuang.zhouzhoubang.ui.page.welfare.shop.ShopWelfareRecordAdapter
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.core.setEmptyData
import com.mufeng.libs.core.setupData
import com.mufeng.libs.core.setupEmptyData
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.vertical

/**
 *  推送订单福利
 */
class PushOrderWelfareActivity : BaseVMActivity<PushOrderWelfareViewModel, ActivityPushOrderWelfareBinding>() {

    companion object {
        fun launch(context: Context){
            context.startPage<PushOrderWelfareActivity>()
        }
    }
    // 福利id
    private val id by lazy {
        intent.getIntExtra("id", 0)
    }
    // 总数量
    private val total by lazy {
        intent.getIntExtra("total", 0)
    }

    // 剩余数量
    private val surplus by lazy {
        intent.getIntExtra("surplus", 0)
    }

    private val data = arrayListOf<PushOrderWelfareBean>()
    private val adapter = PushOrderWelfareAdapter(data)

    private val shopData = arrayListOf<ShopWelfareRecordBean>()
    private val shopAdapter = ShopWelfareRecordAdapter(shopData)
    // 记录类型  0: 平台使用记录  1: 商城使用记录
    private var recordType = 0
    override fun initView(savedInstanceState: Bundle?) {
        binding.titleBar.title = "推送订单"
        binding.titleBar.leftImageView().click { finish() }
        binding.tvTotalNum.text = total.toString()
        binding.tvSurplusNum.text = surplus.toString()
        binding.tvPlatformRecords.isSelected = true
        binding.tvShopRecords.isSelected = false

        binding.tvPlatformRecords.clickWithTrigger {
            binding.tvPlatformRecords.isSelected = true
            binding.tvShopRecords.isSelected = false
            recordType = 0
            binding.refreshLayout.autoRefresh()
            binding.recyclerView.vertical().adapter = adapter
            adapter.setEmptyData()
        }

        binding.tvShopRecords.clickWithTrigger {
            binding.tvPlatformRecords.isSelected = false
            binding.tvShopRecords.isSelected = true
            recordType = 1
            binding.refreshLayout.autoRefresh()
            binding.recyclerView.vertical().adapter = shopAdapter
            shopAdapter.setEmptyData()
        }
        binding.apply {
            recyclerView.vertical().adapter = adapter
            adapter.setEmptyData()
            refreshLayout.setOnRefreshListener {
                p = 1
                isShowing = false
                if(recordType == 0) {
                    vm.getPushOrderLog(p)
                }else{
                    vm.getGoodsWelfareLog(id, p)
                }
            }
            refreshLayout.setOnLoadMoreListener {
                p++
                isShowing = false
                if(recordType == 0) {
                    vm.getPushOrderLog(p)
                }else{
                    vm.getGoodsWelfareLog(id, p)
                }
            }
        }
    }

    override fun initData() {
        binding.refreshLayout.autoRefresh()
    }

    override fun startObserve() {
        vm.getPushOrderLogData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
                binding.refreshLayout.setupEmptyData(data, adapter, p)
            }
            onSuccess {
                dismissLoading()
                binding.refreshLayout.setupData(it, data, adapter, p)
            }
        }

        vm.getGoodsWelfareLogData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
                binding.refreshLayout.setupEmptyData(shopData, shopAdapter, p)
            }
            onSuccess {
                dismissLoading()
                binding.refreshLayout.setupData(it, shopData, shopAdapter, p)
            }
        }
    }

}